package edu.bit.water.service;

import edu.bit.water.dao.MetaStandardRequireDao;
import edu.bit.water.entity.MetaStandardRequire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MetaStandardRequireController的service层
 * author: Page
 */
@Service
public class MetaStandardRequireService {

    @Autowired
    private MetaStandardRequireDao metaStandardRequireDao;

    /**
     * 查询MetaStandardRequire
     */
    public List<MetaStandardRequire> queryStandardRequire(MetaStandardRequire metaStandardRequire) {
        return metaStandardRequireDao.queryStandardRequire(metaStandardRequire);
    }


    /**
     * 插入新的metastandardRequire
     */
    public Map<String, Object> insertMetaStandardRequireService(MetaStandardRequire metaStandardRequire) {
        Map<String, Object> result = new HashMap<>();
        String allcontent = metaStandardRequire.getContent();
        String[] contents = allcontent.split("#");
        for(int i=0;i< contents.length;i++){
            metaStandardRequire.setContent(contents[i]);
//            int num = metaStandardRequireDao.getMaxId();
//            metaStandardRequire.setId(num + 1);
            Integer num = metaStandardRequireDao.getMaxId();
            if (num != null) {
                metaStandardRequire.setId(num + 1);
            }
            else {
                metaStandardRequire.setId(0);
            }
//            System.out.println("num: " + num);
            int tmp = metaStandardRequireDao.insertMetaStandardRequire(metaStandardRequire);
            if (tmp == 1) {
                result.put("status", "success");
            }else {
                result.put("status", "failed");
            }
        }
        return result;
    }


    /**
     * 编辑提交
     */
    public Map<String, Object> updateMetaStandardRequireService(MetaStandardRequire metaStandardRequire) {
//        metaStandardRequire.dateChange();
        Map<String, Object> result = new HashMap<>();
        int tmp = metaStandardRequireDao.update(metaStandardRequire);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 删除metaStandardRequire
     * author： page
     */
    public Map<String, Object> deleteMetaStandardRequires(List<String> metaStandardRequires) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < metaStandardRequires.size(); i++) {
            int result = metaStandardRequireDao.delete(Integer.parseInt(metaStandardRequires.get(i)));
            if(result == 0) {
                tmpMap.put(metaStandardRequires.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(metaStandardRequires.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }
}
